/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jakewharton.rxbinding4.demo.slice;

import com.jakewharton.rxbinding4.ResourceTable;
import com.jakewharton.rxbinding4.component.*;

import io.reactivex.rxjava3.functions.Consumer;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.TextAlignment;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * MainAbilitySlice
 *
 * @since 2021-1-1
 */
public class MainAbilitySlice extends AbilitySlice {
    private static final Logger logger = Logger.getLogger("RxBinding");

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        ComponentContainer root =
                (ComponentContainer)
                        LayoutScatter.getInstance(this).parse(ResourceTable.Layout_layout_main_ability, null, false);
        AbsButton absButton = (AbsButton) root.findComponentById(ResourceTable.Id_toggleButton);
        Component component = root.findComponentById(ResourceTable.Id_component);
        DatePicker datePicker = (DatePicker) root.findComponentById(ResourceTable.Id_datePicker);
        ListContainer listContainer = (ListContainer) root.findComponentById(ResourceTable.Id_listContainer);
        PageSlider pageSlider = (PageSlider) root.findComponentById(ResourceTable.Id_PageSlider);
        Picker picker = (Picker) root.findComponentById(ResourceTable.Id_picker);
        RadioContainer radioContainer = (RadioContainer) root.findComponentById(ResourceTable.Id_radioContainer);
        Rating rating = (Rating) root.findComponentById(ResourceTable.Id_rating);
        Slider slider = (Slider) root.findComponentById(ResourceTable.Id_slider);
        Button btnJump = (Button) root.findComponentById(ResourceTable.Id_btn_jump);
        testAbsButton(absButton);
        testComponent(component);
        testDatePicker(datePicker);
        testListContainer(listContainer);
        testPageSlider(pageSlider);
        testPicker(picker);
        testRadioContainer(radioContainer);
        testRating(rating);
        testSlider(slider);
        btnJump.setClickedListener(component1 -> present(new SampleAbilitySlice(), new Intent()));
        setUIContent(root);
    }

    private void testSlider(Slider view) {
        RxSlider.changes(view)
                .subscribe(
                        new Consumer<Integer>() {
                            @Override
                            public void accept(Integer integer) throws Throwable {
                                logger.log(Level.INFO, "Slider -> changes:" + integer);
                            }
                        });
    }

    private void testRating(Rating view) {
        RxRating.progressbarChange(view)
                .subscribe(
                        new Consumer<Float>() {
                            @Override
                            public void accept(Float aFloat) throws Throwable {
                                logger.log(Level.INFO, "Rating -> progressbarChange:" + aFloat);
                            }
                        });
    }

    private void testRadioContainer(RadioContainer view) {
        RxRadioContainer.markChanged(view)
                .subscribe(
                        new Consumer<RadioContainerCheckedChangedEvent>() {
                            @Override
                            public void accept(RadioContainerCheckedChangedEvent radioContainerCheckedChangedEvent)
                                    throws Throwable {
                                logger.log(Level.INFO, "RadioContainer -> checkedChanged:" + radioContainerCheckedChangedEvent);
                            }
                        });
    }

    private void testPicker(Picker view) {
        RxPicker.valueChanged(view)
                .subscribe(
                        new Consumer<PickerValueChangedEvent>() {
                            @Override
                            public void accept(PickerValueChangedEvent pickerValueChangedEvent) throws Throwable {
                                logger.log(Level.INFO,"Picker -> valueChanged:" + pickerValueChangedEvent);
                            }
                        });
    }

    private void testPageSlider(PageSlider view) {
        view.setProvider(
                new PageSliderProvider() {
                    @Override
                    public int getCount() {
                        return 5;
                    }

                    @Override
                    public Object createPageInContainer(ComponentContainer componentContainer, int i) {
                        Text text = new Text(MainAbilitySlice.this);
                        text.setWidth(ComponentContainer.LayoutConfig.MATCH_PARENT);
                        text.setHeight(ComponentContainer.LayoutConfig.MATCH_PARENT);
                        text.setText(i + "");
                        text.setTextAlignment(TextAlignment.CENTER);
                        componentContainer.addComponent(text);
                        return text;
                    }

                    @Override
                    public void destroyPageFromContainer(ComponentContainer componentContainer, int index, Object obj) {
                        componentContainer.removeComponent(((Component) obj));
                    }

                    @Override
                    public boolean isPageMatchToObject(Component component, Object obj) {
                        return component == obj;
                    }
                });
        RxPageSlider.pageChosen(view)
                .subscribe(
                        new Consumer<Integer>() {
                            @Override
                            public void accept(Integer integer) throws Throwable {
                                logger.log(Level.INFO,"PageSlider -> pageChosen:" + integer);
                            }
                        });

        RxPageSlider.pageSlideStateChanges(view)
                .subscribe(
                        new Consumer<Integer>() {
                            @Override
                            public void accept(Integer integer) throws Throwable {
                                logger.log(Level.INFO,"PageSlider -> pageSlideStateChanges:" + integer);
                            }
                        });

        RxPageSlider.pageSlidingEvents(view)
                .subscribe(
                        new Consumer<PageSliderPageSlidingEvent>() {
                            @Override
                            public void accept(PageSliderPageSlidingEvent pageSliderPageSlidingEvent) throws Throwable {
                                logger.log(Level.INFO,"PageSlider -> pageSlidingEvents:" + pageSliderPageSlidingEvent);
                            }
                        });
    }

    private void testListContainer(ListContainer view) {
        view.setItemProvider(
                new BaseItemProvider() {
                    @Override
                    public int getCount() {
                        return 5;
                    }

                    @Override
                    public Object getItem(int index) {
                        return index;
                    }

                    @Override
                    public long getItemId(int index) {
                        return index;
                    }

                    @Override
                    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
                        Text text = new Text(MainAbilitySlice.this);
                        text.setWidth(ComponentContainer.LayoutConfig.MATCH_PARENT);
                        text.setHeight(50);
                        text.setText(i + "");
                        text.setTextAlignment(TextAlignment.CENTER);
                        return text;
                    }
                });
        RxListContainer.itemClick(view)
                .subscribe(
                        new Consumer<ListContainerItemEvent>() {
                            @Override
                            public void accept(ListContainerItemEvent listContainerItemEvent) throws Throwable {
                                logger.log(Level.INFO,"ListContainer -> itemClick:" + listContainerItemEvent);
                            }
                        });

        RxListContainer.itemLongClick(view)
                .subscribe(
                        new Consumer<ListContainerItemEvent>() {
                            @Override
                            public void accept(ListContainerItemEvent listContainerItemEvent) throws Throwable {
                                logger.log(Level.INFO,"ListContainer -> itemLongClick:" + listContainerItemEvent);
                            }
                        });

        RxListContainer.itemSelected(view)
                .subscribe(
                        new Consumer<ListContainerItemEvent>() {
                            @Override
                            public void accept(ListContainerItemEvent listContainerItemEvent) throws Throwable {
                                logger.log(Level.INFO,"ListContainer -> itemSelected:" + listContainerItemEvent);
                            }
                        });
    }

    private void testDatePicker(DatePicker view) {
        RxDatePicker.valueChanged(view)
                .subscribe(
                        new Consumer<DatePickerValueChangedEvent>() {
                            @Override
                            public void accept(DatePickerValueChangedEvent datePickerValueChangedEvent)
                                    throws Throwable {
                                logger.log(Level.INFO,"DatePicker -> datePickerValueChangedEvent:" + datePickerValueChangedEvent);
                            }
                        });
    }

    private void testComponent(Component view) {
        RxComponent.touches(view)
                .subscribe(
                        new Consumer<Object>() {
                            @Override
                            public void accept(Object o) throws Throwable {
                                logger.log(Level.INFO,"Component -> isTouched");
                            }
                        });
        RxComponent.clicks(view)
                .subscribe(
                        new Consumer<Object>() {
                            @Override
                            public void accept(Object o) throws Throwable {
                                logger.log(Level.INFO,"Component -> isClicked");
                            }
                        });
        RxComponent.longClicks(view)
                .subscribe(
                        new Consumer<Object>() {
                            @Override
                            public void accept(Object o) throws Throwable {
                                logger.log(Level.INFO,"Component -> isLongClicked");
                            }
                        });
    }

    private void testAbsButton(AbsButton view) {
        RxAbsButton.checkedStateChanges(view)
                .subscribe(
                        new Consumer<Boolean>() {
                            @Override
                            public void accept(Boolean checkedState) throws Throwable {
                                logger.log(Level.INFO,"AbsButton -> checkedState:" + checkedState);
                            }
                        });
    }
}
